package com.mz361.common.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class GZipUtils {
    private static int BUFFER = 1024;
    private static String CHARSET = "ISO-8859-1";

    public GZipUtils() {
    }

    public static String compressIso88591(String data) throws Exception {
        byte[] output = (byte[]) null;
        ByteArrayInputStream bais = new ByteArrayInputStream(data.getBytes(CHARSET));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
            compress(bais, baos);
            output = baos.toByteArray();
            baos.flush();
            baos.close();
            bais.close();
        } catch (Exception var5) {
            throw new Exception("压缩失败：" + var5.getMessage());
        }

        return new String(output, CHARSET);
    }

    public static String decompressIso88591(String iso859Data) throws Exception {
        ByteArrayInputStream bais = new ByteArrayInputStream(iso859Data.getBytes(CHARSET));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] output = (byte[]) null;

        try {
            decompress(bais, baos);
            output = baos.toByteArray();
            baos.flush();
            baos.close();
            bais.close();
        } catch (Exception var5) {
            throw new Exception("解压缩失败：" + var5.getMessage());
        }

        return new String(output, CHARSET);
    }

    public static byte[] compress(byte[] data) throws Exception {
        byte[] output = (byte[]) null;
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
            compress(bais, baos);
            output = baos.toByteArray();
            baos.flush();
            baos.close();
            bais.close();
            return output;
        } catch (Exception var5) {
            throw new Exception("压缩失败：" + var5.getMessage());
        }
    }

    private static void compress(InputStream is, OutputStream os) throws Exception {
        GZIPOutputStream gos = new GZIPOutputStream(os);
        byte[] data = new byte[BUFFER];

        int count;
        while ((count = is.read(data, 0, BUFFER)) != -1) {
            gos.write(data, 0, count);
        }

        gos.finish();
        gos.flush();
        gos.close();
    }

    public static byte[] decompress(byte[] data) throws Exception {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
            decompress(bais, baos);
            data = baos.toByteArray();
            baos.flush();
            baos.close();
            bais.close();
            return data;
        } catch (Exception var4) {
            throw new Exception("解压缩失败：" + var4.getMessage());
        }
    }

    private static void decompress(InputStream is, OutputStream os) throws Exception {
        GZIPInputStream gis = new GZIPInputStream(is);
        byte[] data = new byte[BUFFER];

        int count;
        while ((count = gis.read(data, 0, BUFFER)) != -1) {
            os.write(data, 0, count);
        }

        gis.close();
    }
}
